<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Admin;

use Core\Db\Db as Db;
use Model\Admin\OptionModel as OptionModel;

/**
 * 主题模块
 * @author LazyPHP
 * @version 1.0
 */
class Theme extends Common {

    private $enableTheme;
    /**
     * 主题列表
     */
    public function index() {
        $this->enableTheme = OptionModel::getOption('7');
        $list = $this->getThemeList();
        $this->assign('list', $list);
        if(array_key_exists($this->enableTheme['value'], $list) == false){
            $this->assign('lose', $this->enableTheme['value']);
        }
        $this->display();
    }

    /**
     * 获取主题列表
     * @param type $dir 主题目录
     */
    private function getThemeList() {
        //获取当前使用的主题名称
        $dir = "./Theme/Home/";
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    //获取主题列表
                    if (is_dir($dir . $file) && $file != "." && $file != "..") {
                        //获取主题信息
                        if (is_file($dir . $file . '/README.md')) {
                            $content = file($dir . $file . '/README.md');
                            foreach ($content as $key => $value) {
                                $cut = explode(':', $value);
                                if ($cut[0] == 'url') {
                                    $theme[$file]['readme'][$cut[0]] = substr($value, 4);
                                } else {
                                    $theme[$file]['readme'][$cut[0]] = $cut[1];
                                }
                            }
                        } else {
                            $theme[$file]['readme'] = '';
                        }
                        //获取主题版权地址
                        if (is_file($dir . $file . '/LICENSE.txt')) {
                            $theme[$file]['license'] = substr($dir . $file . '/LICENSE.txt', 1);
                        } else {
                            $theme[$file]['license'] = '/LICENSE';
                        }
                        //获取主题截图
                        if (is_file($dir . $file . '/screenshot.png')) {
                            $theme[$file]['thumb'] = substr($dir . $file . '/screenshot.png', 1);
                        } else {
                            $theme[$file]['thumb'] = '/img/lose.png';
                        }
                        //主题是否被启用
                        if ($file == $this->enableTheme['value']) {
                            $theme[$file]['status'] = 1;
                        } else {
                            $theme[$file]['status'] = 0;
                        }
                    }
                }
                closedir($dh);
            }
        }
        return $theme;
    }

    /**
     * 安装主题
     */
    public function install() {
        echo '待开发';
    }
    
    /**
     * 启用主题
     */
    public function enableTheme(){
        $data['value'] = $this->is_g('theme', '请选择您要启用的主题!');
        $db = $this->db('option');
        $updateResult = $db->where('id = 7')->save($data);
        $this->determineSqlExecResult($updateResult, "启用{$data['value']}主题失败！");
        $this->success("成功启用{$data['value']}主题！", '/?g=Admin&m=Theme&a=index');
    }

}
